# Movable Type plugin for filtering duplicate trackback ping.
#
# Release 1.02 (May 30, 2005)
#
# This software is provided as-is. You may use it for commercial or 
# personal use. If you distribute it, please keep this notice intact.
#
# Copyright (c) 2005 Yujiro Araki
#
# Ported to MT 3.2 JunkFilter by Hirotaka Ogawa
# $Id: DuplicatedTBPingLookup.pl 151 2005-10-24 10:13:59Z ogawa $
#
package MT::Plugin::DuplicatedTBPingLookup;
use strict;
use base 'MT::Plugin';
use vars qw($VERSION);
$VERSION = '1.02';

use MT;
use MT::JunkFilter qw(ABSTAIN);

use constant JUNK => 1;
use constant MODERATE => 2;

my $plugin = MT::Plugin::DuplicatedTBPingLookup->new({
    name => 'Duplicated TBPing Lookup',
    description => "Look up duplicated trackback ping from the same source, and mark it as a 'junk'.",
    version => $VERSION,
    blog_config_template => \&template,
    settings => new MT::PluginSettings([
	['dtl_score_weight', { Default => 1 }],
	['dtl_method', { Default => JUNK }],
    ]),
});
MT->add_plugin($plugin);
MT->register_junk_filter({
    name => $plugin->name,
    plugin => $plugin,
    code => sub { $plugin->score(@_) },
});

sub score {
    my ($plugin, $obj) = @_;
    return ABSTAIN unless UNIVERSAL::isa($obj, 'MT::TBPing');

    my $blog_id = $obj->blog_id;
    my $url = $obj->source_url;
    $url =~ s/^\s+|\s+$//gs;

    my $iter = MT::TBPing->load_iter({ blog_id => $blog_id,
				       tb_id => $obj->tb_id },
				     { sort => 'created_on',
				       direction => 'descend' });
    while (my $ping = $iter->()) {
	if ($ping->source_url eq $url) {
	    my $config = $plugin->get_config_hash("blog:$blog_id");
	    my $method = $config->{dtl_method} || JUNK;
	    my $weight = $config->{dtl_score_weight};
	    $weight = 1 unless defined $weight;
	    my $msg = "Duplicated trackback ping from the same source: $url";
	    if ($method == JUNK) {
		return (-1 * $weight, $msg);
	    } elsif ($method == MODERATE) {
		$obj->moderate;
		return (0, $msg);
	    }
	}
    }
    ABSTAIN;
}

sub template {
    my $static_img_uri = MT->instance->static_path . 'images';
    my $tmpl = <<EOT;
<script type="text/javascript">
<!--
function dtlScoreNudge(amount, id) {
    var fld = getByID(id);
    score = fld.value;
    score.replace(/\\+/, '');
    score = parseFloat(score) + amount;
    if (isNaN(score)) score = amount;
    if (score > 10) score = 10;
    if (score < 0) score = 0;
    fld.value = score;
    return false;
}
//-->
</script>

<p>This plugin allows you to look up duplicated trackback ping from the same source and mark it as a 'junk'.</p>
<div class="setting">
<div class="label"><label>Score Weight:</label></div>
<div class="field">
<a href="#" class="spinner" onclick="return dtlScoreNudge(-1, 'dtl_score_weight')"><img src="$static_img_uri/decrease.gif" alt="<MT_TRANS phrase="Decrease">" width="12" height="8" /></a>
<input type="text" size="3" name="dtl_score_weight" id="dtl_score_weight" value="<TMPL_VAR NAME=DTL_SCORE_WEIGHT ESCAPE=HTML>" />
<a href="#" class="spinner" onclick="return dtlScoreNudge(1, 'dtl_score_weight')"><img src="$static_img_uri/increase.gif" alt="<MT_TRANS phrase="Increase">" width="12" height="8" /></a>
</div>
</div>
<div class="setting">
<div class="label"><label>Action:</label></div>
<div class="field">
<ul>
<li><label><input type="radio" name="dtl_method" value="1"<TMPL_IF NAME=DTL_METHOD_1> checked="checked"</TMPL_IF> /> Junk</label></li>
<li><label><input type="radio" name="dtl_method" value="2"<TMPL_IF NAME=DTL_METHOD_2> checked="checked"</TMPL_IF> /> Not Junk, but Moderated</label></li>
</ul>
</div>
</div>
EOT
}

1;
